package com.ruoyi.project.tool.export;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.mgt.SessionManager;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

public class HtmlToWord{

    public AjaxResult writeWordFile(String html, String path, String fileName) {
        //String path = "c:/";
        //String fileName = "a.docx";
        String realFileName = encodingFilename(fileName);
        try {
            if (!"".equals(path)) {

                // 检查目录是否存在
                File fileDir = new File(path);
                if (fileDir.exists()) {
                    // 生成临时文件名称
                    byte b[] = html.getBytes("GBK");
                    ByteArrayInputStream bais = new ByteArrayInputStream(b);
                    POIFSFileSystem poifs = new POIFSFileSystem();

                    DirectoryEntry directory = poifs.getRoot();
                    DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
                    OutputStream out = new FileOutputStream(getAbsoluteFile(realFileName));
                    poifs.writeFilesystem(out);
                    bais.close();
                    out.close();
                    /*HttpServletResponse response= ServletUtils.getResponse();
                    response.setContentType("multipart/form-data");
                    response.setHeader("Content-Disposition", "attachment;fileName=" + "aaa.doc");
                    OutputStream out = ServletUtils.getResponse().getOutputStream();*/
                    /*HttpServletResponse response= ServletUtils.getResponse();
                    response.reset();
                    response.setContentType("application/x-download");
                    response.addHeader("Content-Disposition", "attachment; filename=\"" + "checkrepositorytemplate.doc" + "\"");

                    ServletOutputStream servletOS = response.getOutputStream(); //输出流
                    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
                    poifs.writeFilesystem(ostream);
                    servletOS.write(ostream.toByteArray());

                    servletOS.flush();
                    ostream.close();
                    servletOS.close();
                     */
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("未知错误，下载失败！");
        }finally {

        }
        return AjaxResult.success(realFileName);
    }

   /* public static void main(String[] args){
        String path = "d:/";
        String fileName = "a.doc";
        String html = "<html>" +
                "<head>你好</head>" +
                "<body>" +
                "<table>" +
                "<tr>" +
                "<td>信息1</td>" +
                "<td>信息2</td>" +
                "<td>t3</td>" +
                "<tr>" +
                "</table>" +
                "</body>" +
                "</html>";
        writeWordFile(html,path,fileName);
    }*/

    public String getAbsoluteFile(String filename)
    {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }
    public String encodingFilename(String filename)
    {
        filename = UUID.randomUUID().toString() + "_" + filename + ".doc";
        return filename;
    }
}
